package com.example.kai.uno;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Login extends AppCompatActivity {
    private MyDBHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        dbHelper = new MyDBHelper(this, "UserStore.db", null, 1);

        Button botton = findViewById(R.id.button);

        final EditText editText = findViewById(R.id.editText);
        final EditText editText1 = findViewById(R.id.editText2);

        ImageView imageView = (ImageView)findViewById(R.id.imageView);
        ObjectAnimator moveIn = ObjectAnimator.ofFloat(imageView, "translationX", -500f, 0f);
        ObjectAnimator rotate = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
        ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f, 1f);
        AnimatorSet animSet = new AnimatorSet();
        animSet.play(rotate).with(fadeInOut).after(moveIn);
        animSet.setDuration(3000);
        animSet.start();
       ;

        botton.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
                                          String userName = editText.getText().toString();
                                          String passWord = editText1.getText().toString();
                                          if (login(userName, passWord)) {
                                              Toast.makeText(Login.this,"登录成功",Toast.LENGTH_LONG).show();
                                              Intent intent = new Intent(Login.this,Startgame.class);
                                              startActivity(intent);
                                          } else {
                                              Toast.makeText(Login.this, "登陆失败",Toast.LENGTH_LONG).show();
                                          }


                                      }
                                  }
        );
        TextView textView=(TextView)findViewById(R.id.textView3);
        String text1="注册";
        SpannableString spannableString1=new SpannableString(text1);
        spannableString1.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Login.this,Register.class);
                startActivity(intent);
                }
        }, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString1);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        TextView textView1=(TextView)findViewById(R.id.textView4);
        String text2="忘记密码";
        SpannableString spannableString=new SpannableString(text2);

        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Login.this,ResetPasswords.class);
                startActivity(intent);

            }
        }, 0, text2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        textView1.setText(spannableString);
        textView1.setMovementMethod(LinkMovementMethod.getInstance());
        }
    public boolean login(String username, String password) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "select * from userData where name=? and password=?";
        Cursor cursor = db.rawQuery(sql, new String[]{username, password});
        if (cursor.moveToFirst()) {
            cursor.close();
            return true;
        }
        else
            return false;
    }
}

